package com.wwj.airline.Controller;

import com.wwj.airline.pojo.Flight;
import com.wwj.airline.Dao.FlightRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/flight")
public class FilghtHandler {
    @Autowired(required = false)
    private FlightRepository flightRepository;

    @GetMapping("/findAll")
    public List<Flight> findAll(){
        return flightRepository.findAll();
    }

    @GetMapping("/findById/{id}")
    public Flight findById(@PathVariable("id") Long id){
        return flightRepository.findById(id);
    }

    @GetMapping("/findByFromCity/{fromCity}")
    public List<Flight> findByFromCity(@PathVariable("fromCity") String fromCity){
        return flightRepository.findByFromCity(fromCity);
    }

    @GetMapping("/findByToCity/{toCity}")
    public List<Flight> findByToCity(@PathVariable("toCity") String toCity){
        return flightRepository.findByToCity(toCity);
    }

    @GetMapping("/findByFTCity/{fCity}/{tCity}")
    public List<Flight> findByFTCity(@PathVariable("fCity") String fCity,@PathVariable("tCity") String tCity){
        return flightRepository.findByFTCity(fCity,tCity);
    }

    @GetMapping("/findByFromTime/{fromTime}")
    public List<Flight> findByFromTime(@PathVariable("fromTime") String fromTime){
        return flightRepository.findByFromTime(fromTime);
    }

    @PostMapping("/save")
    public void save(@RequestBody Flight flight){
        flightRepository.save(flight);
    }

    @PutMapping("/update")
    public void update(@RequestBody Flight flight){
        flightRepository.update(flight);
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") Long id){
        flightRepository.deleteById(id);
    }
}
